package com.addplus.townmall.server.api.mapper.town;

import com.addplus.townmall.server.api.model.town.Coupon;
import com.addplus.townmall.server.api.model.town.expand.CouponExpand;
import com.addplus.townmall.server.api.model.town.expand.AreaExpand;
import com.addplus.townmall.server.api.utils.BaseAddMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * 优惠卷 Mapper 接口
 * </p>
 *
 * @author zhangjiehang
 * @since 2018-08-27
 */
public interface CouponMapper extends BaseAddMapper<Coupon> {

    @Select("<script>\n" +
            "SELECT\n" +
            " c.id,\n" +
            " c.coupon_name,\n" +
            " c.coupon_describe,\n" +
            " c.store_id,\n" +
            " c.coupon_type,\n" +
            " c.discount_type,\n" +
            " c.min_price,\n" +
            " c.sub_price,\n" +
            " c.discount,\n" +
            " c.user_num\n" +
            " <if test=\"memberId != null and memberId !=''\">\n" +
            " ,count(r.id) as receive_count\n" +
            " </if>\n" +
            "FROM\n" +
            " coupon c\n" +
            " <if test=\"memberId != null and memberId !=''\">\n" +
            " LEFT JOIN coupon_member_record r ON r.coupon_id = c.id \n" +
            " AND r.is_delete = 0 \n" +
            " AND r.member_id = #{memberId} \n" +
            " </if>\n" +
            "WHERE\n" +
            " c.is_delete = 0 \n" +
            " AND c.show_location = 1 \n" +
            " AND c.online_status = 1 \n" +
            " AND c.coupon_type = #{couponType}\n" +
            "<if test=\"memberId != null and memberId !=''\">\n" +
            "GROUP BY\n" +
            " c.id\n" +
            "</if>\n" +
            "ORDER BY\n" +
            " <if test=\"couponType == 1\">\n" +
            " c.receive_num desc,\n" +
            " </if>\n" +
            " <if test=\"couponType != 1\">\n" +
            " c.sort,\n" +
            " </if>\n" +
            " c.gmt_create desc\n" +
            "</script>")
    List<CouponExpand> getShopCouponCenterByPage(Page page, @Param("couponType") Integer couponType, @Param("memberId") Integer memberId);


    @Select("<script>\n" +
            "SELECT\n" +
            " c.id,\n" +
            " c.coupon_name,\n" +
            " c.coupon_describe,\n" +
            " c.store_id,\n" +
            " c.coupon_type,\n" +
            " c.discount_type,\n" +
            " c.min_price,\n" +
            " c.sub_price,\n" +
            " c.discount,\n" +
            " c.user_num,\n" +
            " c.integral\n" +
            " <if test=\"memberId != null and memberId !=''\">\n" +
            " ,count(r.id) as receive_count\n" +
            " </if>\n" +
            "FROM\n" +
            " coupon c\n" +
            " <if test=\"memberId != null and memberId !=''\">\n" +
            " LEFT JOIN coupon_member_record r ON r.coupon_id = c.id \n" +
            " AND r.is_delete = 0 \n" +
            " AND r.member_id = #{memberId} \n" +
            " </if>\n" +
            "WHERE\n" +
            " c.is_delete = 0 \n" +
            " AND c.show_location = 2 \n" +
            " AND c.online_status = 1 \n" +
            " AND c.integral is not null \n" +
            "<if test=\"memberId != null and memberId !=''\">\n" +
            "GROUP BY\n" +
            " c.id\n" +
            "</if>\n" +
            "ORDER BY\n" +
            " c.sort,\n" +
            " c.gmt_create desc\n" +
            "</script>")
    List<CouponExpand> getIntegralCouponCenterByPage(Page page, @Param("memberId") Integer memberId);

    @Select("<script>\n" +
            "SELECT\n" +
            " c.id,\n" +
            " c.coupon_name,\n" +
            " c.coupon_describe,\n" +
            " c.store_id,\n" +
            " c.coupon_type,\n" +
            " c.discount_type,\n" +
            " c.min_price,\n" +
            " c.sub_price,\n" +
            " c.discount,\n" +
            " c.user_num\n" +
            " <if test=\"memberId != null and memberId !=''\">\n" +
            " ,count(r.id) as receive_count\n" +
            " </if>\n" +
            "FROM\n" +
            " coupon c\n" +
            " <if test=\"memberId != null and memberId !=''\">\n" +
            " LEFT JOIN coupon_member_record r ON r.coupon_id = c.id \n" +
            " AND r.is_delete = 0 \n" +
            " AND r.member_id = #{memberId} \n" +
            " </if>\n" +
            "WHERE\n" +
            " c.is_delete = 0 \n" +
            " AND c.show_location = 3 \n" +
            " AND c.online_status = 1 \n" +
            " AND c.coupon_type = #{couponType}\n" +
            " AND c.store_id = #{storeId}\n" +
            "<if test=\"memberId != null and memberId !=''\">\n" +
            "GROUP BY\n" +
            " c.id\n" +
            "</if>\n" +
            "ORDER BY\n" +
            " c.sort,\n" +
            " c.gmt_create desc\n" +
            "</script>")
    List<CouponExpand> getShopDescribeCouponByPage(Page page, @Param("couponType") Integer couponType, @Param("memberId") Integer memberId, @Param("storeId") Integer storeId);

    @Select("SELECT * FROM coupon WHERE coupon_type=1 AND is_delete = 0 ORDER BY sort")
    List<Coupon> getshopCouponList();

}
